package com.example.healthsystem.controller;


import com.example.healthsystem.entity.healthreg;
import com.example.healthsystem.entity.user;
import com.example.healthsystem.mapper.UserMapper;
import com.example.healthsystem.service.HealthregService;
import com.example.healthsystem.service.ReglistService;
import com.example.healthsystem.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * USERCONTROLLER包含 ADMIN、COMMON USER(STUDNET/TEACHER) 两大类，因此需要分别对两个不同权限所执行的功能进行划分
 * 普通路由跳转方法
 * 返回RESPONSEBODY 的信息的方法
 */
@RequestMapping("/user")
//@RestController
@Controller // --- user thymeleaf
// --- 向模板返回数据必须要使用 Model.model
public class UserController {

    @Autowired
    HealthregService healthregService;
    @Autowired
    ReglistService reglistService;
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    // ---------------------------------------------------------------------------------
    // --------------------------------- 以下进行页面跳转 ---------------------------------
    // ---------------------------------------------------------------------------------

    /**
     * 跳转到 用户个人信息页面
     * @return
     */
    @RequestMapping("/profile")
    public String profile(Model model,String username){
        user user= userService.finduserByusername(username);
        model.addAttribute("user",user);
        return "/user/user/profile";
    }
    /**
     * 跳转到用户修改信息页面
     * @param model
     * @param username
     * @param name
     * @return
     */
    @RequestMapping("/change_msg")
    public String change_msg(Model model,
                             @RequestParam String username,
                             @RequestParam String name) {
        model.addAttribute("username",username);
        model.addAttribute("name",name);
        return "/user/user/change_msg";
    }


    /**
     * 跳转到 用户健康登记页面
     * @return
     */
    @RequestMapping("/reginfonow")
    public String reginfonow(Model model,String username,String name){
        model.addAttribute("username",username);
        model.addAttribute("name",name);
        // --- 用户后面的传参操作
        return "/user/healthreg/reginfonow";
    }

    @RequestMapping("/reginfoupload")
    public String reginfoupload(){
        return "/user/healthreg/reginfoupload";
    }

    @RequestMapping("/reginfolook")
    public String reginfolook(){
        return "/user/healthreg/reginfolook";
    }

    @RequestMapping("/reginfochange")
    public String reginfochange(){
        return "/user/healthreg/reginfochange";
    }

    /**
     * 跳转到 用户历史登记页面
     * @return
     */
    @RequestMapping("/reginfobef")
    public String reginfobef(Model model,String name){
        model.addAttribute("name",name);
        return "/user/healthregbef/reginfobef";
    }



    // ---------------------------------------------------------------------------------
    // ------------------------------- 【用户】进行相关操作 -------------------------------
    // ---------------------------------------------------------------------------------

    // --- 【返回页面】 ---
    // ==================
    /**
     * insert
     * 插入健康登记表
     * @param healthreg
     * @return
     */
    @PostMapping("/insertHealth")
    public String insertHealth(@RequestBody healthreg healthreg){
        healthregService.insertHealth(healthreg);
        return "true";
    }

    /**
     * delete
     * 删除健康登记信息
     * @param name
     * @return
     */
    @DeleteMapping("/deleteReginfo")
    public String deleteReginfo(@RequestParam String name){
        healthregService.deleteReginfo(name);
        return "/user/user/profile";
    }

    /**
     * update
     * 修改登记信息
     * @param name
     * @param healthreg
     * @return
     */
    @PutMapping("/changeReginfo")
    public String changeReginfo(@RequestParam String name,
                                @RequestParam healthreg healthreg){
        healthregService.changeReginfo(name, healthreg);
        return "/user/healthreg/reginfonow";
    }

    /**
     * update
     * 修改未登记表的未登记次数
     * 只修改 unregnum 这一个量
     * @param name
     * @param unregnum
     * @param lastregtime
     */
    @PutMapping("/changeList")
    public void changeList(@RequestParam String name,
                           @RequestParam int unregnum,
                           @RequestParam Timestamp lastregtime){
        reglistService.changeList(name, unregnum, lastregtime);
    }

    /**
     * update
     * 用户（教师、学生） 修改个人信息
     * 修改内容（用户名、密码）
     * @param user
     * @return
     */
    @RequestMapping("/changeMyMsg")
    public String changeMyMsg(user user) {
        if(user.getPassword()!=null&&!"".equals(user.getPassword())){
            return "redirect:/";
        }
        userService.userchangeProfile(user);
        return "redirect:/user/user/profile";
    }

    /**
     * select
     * 根据 name time 字段搜索具体某一天的健康登记表
     * @param model
     * @param name
     * @param time
     * @return
     */
    @GetMapping("/searchHealthDate")
    public String searchHealthDate(Model model,
                                   @RequestParam String name,
                                   @RequestParam Timestamp time){
        healthreg healthreg=healthregService.searchHealthDate(name, time);
        model.addAttribute("healthreg",healthreg);
        return "";
    }

    /**
     * select
     * 通过 username 字段搜索用户信息
     * @param model
     * @param username
     * @return
     */
    @GetMapping("/searchUser")
    public String searchUserinfo(Model model,String username){
        user user= userMapper.findByUsername(username);
        model.addAttribute("user",user);
        return "admin/user/user_msg";
    }

    // --- 【返回响应数据】 ---
    // ==================

    /**
     * select\post
     * 返回相应数据
     * 依据 [name] 字段搜索用户相关信息
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/healthper")
    @ResponseBody
    public PageInfo<healthreg> searchHealthPer(@RequestParam String name,
                                               @RequestParam(defaultValue = "1") int pageNum,
                                               @RequestParam(defaultValue = "10") int pageSize) {
        List<healthreg> healthreg=healthregService.searchHealthPer(name,pageNum,pageSize);
        return new PageInfo<>(healthreg);
    }
}
